<template>
  <div style="height: 100%; background: #fff">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="6">
            <a-form-item label="考试名称：" name="name">
              <a-input placeholder="" v-model:value="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item label="考试类型：" name="openType">
              <a-select v-model:value="queryParam.openType">
                <a-select-option value="完全公开">完全公开</a-select-option>
                <a-select-option value="部门开发">部门开发</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item label="考试状态：" name="status">
              <a-select v-model:value="queryParam.status">
                <a-select-option value="未发布">未发布</a-select-option>
                <a-select-option value="开发中">开发中</a-select-option>
                <a-select-option value="已结束">已结束</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus"> </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
                <a @click="toggleSearchStatus = !toggleSearchStatus" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <Icon :icon="toggleSearchStatus ? 'ant-design:up-outlined' : 'ant-design:down-outlined'" />
                </a>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <LabExamModal @register="registerModal" @success="handleSuccess"></LabExamModal>
  </div>
</template>

<script lang="ts" name="exam-labExam" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './LabUserExam.data';
  import { list, queryByExamId } from './LabUserExam.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useModal } from '/@/components/Modal';
  import LabExamModal from './LabUserExamForm.vue';
  import { message } from 'ant-design-vue';

  const formRef = ref();
  const queryParam = reactive<any>({});
  const toggleSearchStatus = ref<boolean>(false);
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '考试信息主表',
      api: list,
      columns,
      canResize: false,
      useSearchForm: false,
      showIndexColumn: true,
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  });
  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });

  //注册model
  const [registerModal, { openModal }] = useModal();
  /**
   * 编辑事件
   */
  async function handleEdit(record) {
    console.log('考试', record);
    const exam_id = record.id;
    const paper = await queryByExamId({ examId: exam_id });
    console.log(paper);
    if (paper != null) {
      const state = paper.state;
      if (state == '已结束') {
        message.error('本次考试已完成，不能再次考试！');
      } else {
        openModal(true, {
          id: record.id,
        });
      }
    } else {
      openModal(true, {
        id: record.id,
      });
    }
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    if (record.status == '已结束') {
      return [];
    } else if (record.status == '开放中') {
      return [
        {
          label: '去考试',
          onClick: handleEdit.bind(null, record),
        },
      ];
    }
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table {
    min-height: auto;
  }
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }

    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }

    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }

  :deep(.ant-table-title) {
    display: none;
  }
</style>
